package cn.sfa.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class SQLConnUtil {
	
	private static InputStream inStream;
	private static Properties pro;
	private static DataSource datasource;
	
	static{
		Resource resource = new ClassPathResource("/dbcpconfig.properties");

		try {
			inStream=resource.getInputStream();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		pro=new Properties();
		try {
			pro.load(inStream);
			datasource=BasicDataSourceFactory.createDataSource(pro);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("初始化错误！请核对配置文件dbcpconfig.properties是否放在src目录下");
		}
	}
	
	public static DataSource getDataSource(){
		return datasource;
	}
	
	public static Connection getConnection(){
		try {
			return datasource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("得到数据库连接失败！");
		}
	}
	
	public static void free(ResultSet rs,Statement sta,Connection conn){
		try {
			if(rs!=null){
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(sta!=null){
					sta.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(conn!=null){
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
